package com.yiren.dbaa.module.practice_set.carmodel.view;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.dialog.CommonDialog;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.module.practice_set.carmodel.contract.CarModelHomeContract;
import com.yiren.dbaa.module.practice_set.carmodel.presenter.CarModelHomePresenter;
import com.yiren.dbaa.module.practice_set.carmodel.view.activity.AddCarModelActivity;
import com.yiren.dbaa.module.practice_set.carmodel.view.activity.CarModelManageActivity;
import com.yiren.dbaa.module.practice_set.carmodel.view.adapter.CarModelHomeAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/12/5 8:56
 * @description: 车辆模型首页
 */
public class CarModelHomeFragment extends LazyLoadFragment implements CarModelHomeContract.View {

    @BindView(R.id.lv_carmodel_data)
    ListView lvCarmodelData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.ll_add_car_model)
    LinearLayout llAddCarModel;

    private CarModelHomeContract.Presenter carHomePresenter;
    private boolean isPartner;//是否是合伙人视角

    @Override
    protected int setContentView() {
        return R.layout.fragment_carmodel_home;
    }

    @Override
    protected void init() {
        isPartner = LoadConfigApi.loadUserParamConfig(getActivity()).isPartner();
        carHomePresenter = new CarModelHomePresenter(getActivity());
        carHomePresenter.loadCarModelList(this);
        if (isPartner) {
            llAddCarModel.setVisibility(View.VISIBLE);
        } else {
            llAddCarModel.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        carHomePresenter.loadCarModelList(this);
    }

    @OnClick({R.id.ll_add_car_model})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_add_car_model:
                startActivity(new Intent(getActivity(), AddCarModelActivity.class));
                break;
        }
    }

    @Override
    public void showCarModel(List<CarModelEntity> carList) {
        if (carList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            lvCarmodelData.setVisibility(View.GONE);
            tvTips.setText("您当前车模列表为空，可查看公开车模中是否存在符合车型，不存在则可自己新增车模！");
        } else {
            llNoData.setVisibility(View.GONE);
            lvCarmodelData.setVisibility(View.VISIBLE);
            CarModelHomeAdapter carHomeListAdapter = new CarModelHomeAdapter(getActivity(), carList);
            lvCarmodelData.setAdapter(carHomeListAdapter);
            carHomeListAdapter.setOnManageClickListener((view, position) -> {
                if (carList.get(position).isDownLoad()) {
                    if (isPartner) {
                        Intent intent = new Intent(getActivity(), CarModelManageActivity.class);
                        intent.putExtra("carModel", carList.get(position));
                        startActivity(intent);
                    }
                } else {
                    carHomePresenter.downLoadCarModel(carList.get(position), CarModelHomeFragment.this);
                }
            });

            carHomeListAdapter.setOnDeleteClickListener((view, position) -> {
                CommonDialog.showTwoBtn(getActivity(), "操作确认", "确定删除" + carList.get(position).getName() + "吗？", "取消", "确认", (dialog, which) -> {
                    if (which == -2) {
                        dialog.dismiss();
                    } else {
                        carHomePresenter.deleteCarModel(carList.get(position), new CommonCallBack() {
                            @Override
                            public void success(Object result) {
                                carList.remove(position);
                                carHomeListAdapter.notifyDataSetChanged();
                                dialog.dismiss();
                            }
                        });
                    }
                });
            });
        }
    }

    @Override
    public void refreshCarModelList() {
        carHomePresenter.loadCarModelList(this);
    }
}
